package olegroshka.src.com.bofa.olegroshka.report;

import java.io.PrintStream;

import olegroshka.src.com.bofa.olegroshka.format.RecordFormatter;

public class SimpleReportPrinter implements ReportPrinter<Object[], Object[], Object[]> {

	private final PrintStream[] printStreams; 
	
	private final RecordFormatter[] headerFormatters;
	private final RecordFormatter recordFormatter;
	private final RecordFormatter[] footerFormatters;
	
	public SimpleReportPrinter(
			PrintStream[] printStreams,
			RecordFormatter[] headerFormatters,
			RecordFormatter recordFormatter, 
			RecordFormatter[] footerFormatters) {
		this.printStreams = printStreams;
		this.headerFormatters = headerFormatters;
		this.recordFormatter = recordFormatter;
		this.footerFormatters = footerFormatters;
	}

	public void writeHeader(Object[] headerData) {
		for (int i = 0; i < printStreams.length; i++) {
			for (int j = 0; j < headerFormatters.length; j++) {
				printStreams[i].println(headerFormatters[j].decorate(headerData));
			}
		}
	}

	public void writeData(Object[] record) {
		for (int i = 0; i < printStreams.length; i++) {
			printStreams[i].println(recordFormatter.decorate(record));
		}
	}

	public void writeFooter(Object[] footerData) {
		for (int i = 0; i < printStreams.length; i++) {
			PrintStream ps = printStreams[i];
			for (int j = 0; j < footerFormatters.length; j++) {
				ps.println(footerFormatters[j].decorate(footerData));
			}
			ps.flush();
		}
	}
	
}
